package com.archgeek.java.inoutstream;

import org.junit.Before;
import org.junit.Test;

import java.io.*;

public class DataOutputStreamTest {


    @Test
    public void write() {
        String file = "/Users/pizhihui/ProjectsIdea/code-bigdata/code-00java/src/main/java/com/archgeek/java/inoutstream/1.txt";
        try (DataOutputStream out = new DataOutputStream(new FileOutputStream(new File(file)))) {

            String[] names = {"张三", "李四", "王五"};
            int[] ages = {23, 18, 15};

            for (int i = 0; i < names.length; i++) {
                out.writeChars(names[i]);
                out.writeChar('\t');
                out.writeInt(ages[i]);
                out.writeChar('\n');
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void read() {
        String file = "/Users/pizhihui/ProjectsIdea/code-bigdata/code-00java/src/main/java/com/archgeek/java/inoutstream/1.txt";
        try (DataInputStream in = new DataInputStream(new FileInputStream(new File(file)))) {

            char c = 0;
            char[] tmp = null;
            int len = 0;

            while (in.available()!=0) {
                // System.out.println("available=" +in.available());
                tmp=new char[200];
                len = 0;
                while ((c = in.readChar())!='\t'){
                    tmp[len++] = c;
                }
                String name = new String(tmp, 0, len);
                int age = in.readInt();
                char lr = in.readChar();
                System.out.printf(lr + "name=" + name + "..age=" + age);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
